<?php
use GFPHP\Config,GFPHP\Db;
if (!defined('__ROOT__')) exit('Sorry,Please from entry!');

/**
 * Class PdoDriver
 */
class PdoDriver extends Db
{
    private $db;

    /**
     * @return bool
     */
    public function connect()
    {
        try {
//            $this->db = new pdo(config::database('DSN'), Config::database('user'), Config::database('pass'),array(PDO::ATTR_PERSISTENT => true));
            $this->db = new pdo(config::database('DSN'), Config::database('user'), Config::database('pass'));
        } catch (PDOException $e) {
            echo ('链接数据库时出现错误:<font color="red">' . $e->getMessage() . '</font>');
        }
        if (!$this->db)
            return false;
        else {
            $this->exec('set names ' . Config::database('charset'));
            return true;
        }
    }

    /**
     * 数据库驱动必须创建下列方法
     * 并且必须返回正确的值
     * @param $sql
     * @return array
     */
    public function _query($sql)
    {
        $query = $this->db->query($sql);
        if(!$query)
            return array();
        $result = $query->fetchAll(PDO::FETCH_ASSOC);   //只获取键值
		unset($query);
        return $result;

    }

    /**
     * @param $sql
     * @return mixed
     */
    public function _exec($sql)
    {
        return $this->db->exec($sql);
    }

    public function beginTransaction()
    {
        $this->db->beginTransaction();
    }

    public function commit()
    {
        $this->db->commit();
    }

    public function rollBack()
    {
        $this->db->rollBack();
    }

    public function close()
    {

    }
}
